package com.echofa.sealedghost.volleydemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import nettools.XmlRequest;


public class MainActivity extends AppCompatActivity {

    private Button   btnReport  = null;
    private EditText etValue    = null;
    private EditText etResult   = null;
    private Button   btnRegister= null;
    private RequestQueue  mQueue  = null;

    private static  final String RequestUrl  = "192.168.1.116";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        mQueue  = Volley.newRequestQueue(MainActivity.this);
    }


    private void initView(){
        btnReport  = (Button)findViewById(R.id.btn_report);
        btnRegister = (Button)findViewById(R.id.btn_register);
        etResult   = (EditText) findViewById(R.id.et_result);
        etValue  = (EditText)findViewById(R.id.et_value);

        etValue.setEnabled(false);
        btnReport.setEnabled(false);

        Log.d("TAG", "Init view done");

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                register();
            }
        });


        btnReport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String valueString  = etValue.getText().toString();
                if(valueString.length() > 0){
                    int value  =  Integer.parseInt(etValue.getText().toString());
                    report(value);
                }
            }
        });
    }


    private  void register() {

        StringRequest stringRequest  = new StringRequest(RequestUrl,
                new Response.Listener<String>(){
                    @Override
                    public void onResponse(String response){
                        Log.d("TAG", response);
                    }
                },new Response.ErrorListener(){
                    @Override
                    public void onErrorResponse(VolleyError error){
                        Log.e("TAG", error.getMessage(), error);
                    }
                });

        mQueue.add(stringRequest);

    }


    private void report(int value){

    }
}
